<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>作业 - AI智能教学培训系统</title>
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', 'Microsoft YaHei', sans-serif;
        }

        :root {
            --primary: #4361ee;
            --secondary: #3f37c9;
            --accent: #4895ef;
            --light: #f8f9fa;
            --dark: #212529;
            --success: #4cc9f0;
            --warning: #f72585;
            --gray: #6c757d;
        }

        body {
            background-color: #f5f7fb;
            color: var(--dark);
        }

        .app-container {
            display: flex;
            min-height: 100vh;
        }

        /* 侧边栏样式 */
        .sidebar {
            width: 260px;
            background: linear-gradient(180deg, var(--primary), var(--secondary));
            color: white;
            box-shadow: 3px 0 10px rgba(0, 0, 0, 0.1);
            transition: all 0.3s;
            position: fixed;
            height: 100vh;
            overflow-y: auto;
        }

        .logo-container {
            padding: 20px;
            text-align: center;
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
        }

        .logo {
            font-size: 24px;
            font-weight: 700;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .logo-icon {
            margin-right: 10px;
            font-size: 28px;
        }

        .nav-menu {
            padding: 20px 0;
        }

        .nav-item {
            padding: 12px 25px;
            display: flex;
            align-items: center;
            cursor: pointer;
            transition: all 0.3s;
            border-left: 4px solid transparent;
        }

        .nav-item:hover {
            background: rgba(255, 255, 255, 0.1);
            border-left: 4px solid var(--accent);
        }

        .nav-item.active {
            background: rgba(255, 255, 255, 0.1);
            border-left: 4px solid var(--accent);
        }

        .nav-icon {
            margin-right: 12px;
            font-size: 18px;
        }

        /* 主内容区 */
        .main-content {
            flex: 1;
            margin-left: 260px;
            padding: 30px;
            transition: all 0.3s;
        }

        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 30px;
        }

        .page-title {
            font-size: 28px;
            font-weight: 600;
            color: var(--dark);
        }

        .user-info {
            display: flex;
            align-items: center;
        }

        .avatar {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            background: var(--accent);
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-weight: bold;
            margin-right: 10px;
        }

        /* 任务页面特定样式 */
        .task-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 25px;
        }

        .task-tabs {
            display: flex;
            background: #f0f2f5;
            border-radius: 8px;
            padding: 5px;
        }

        .task-tab {
            padding: 8px 20px;
            border-radius: 6px;
            cursor: pointer;
            font-size: 14px;
            transition: all 0.3s;
        }

        .task-tab.active {
            background: white;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
            font-weight: 500;
            color: var(--primary);
        }

        .task-add-btn {
            background: var(--primary);
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 6px;
            cursor: pointer;
            display: flex;
            align-items: center;
            transition: all 0.3s;
        }

        .task-add-btn:hover {
            background: var(--secondary);
        }

        .task-add-icon {
            margin-right: 8px;
        }

        .task-list {
            display: flex;
            flex-direction: column;
            gap: 15px;
        }

        .task-card {
            background: white;
            border-radius: 12px;
            padding: 20px;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
            transition: all 0.3s;
            position: relative;
        }

        .task-card:hover {
            transform: translateY(-3px);
            box-shadow: 0 8px 20px rgba(0, 0, 0, 0.1);
        }

        .task-card.urgent {
            border-left: 4px solid var(--warning);
        }

        .task-card.completed {
            opacity: 0.8;
            background: #f8f9fa;
        }

        .task-title {
            font-size: 18px;
            font-weight: 600;
            margin-bottom: 10px;
            display: flex;
            align-items: center;
        }

        .task-title .checkbox {
            margin-right: 12px;
            cursor: pointer;
        }

        .task-meta {
            display: flex;
            gap: 20px;
            margin-bottom: 15px;
            color: var(--gray);
            font-size: 14px;
        }

        .task-meta-item {
            display: flex;
            align-items: center;
        }

        .task-meta-icon {
            margin-right: 6px;
            font-size: 16px;
        }

        .task-course {
            display: inline-block;
            padding: 4px 10px;
            background: rgba(67, 97, 238, 0.1);
            color: var(--primary);
            border-radius: 20px;
            font-size: 12px;
            margin-bottom: 15px;
        }

        .task-description {
            color: var(--gray);
            margin-bottom: 15px;
            line-height: 1.6;
        }

        .task-progress {
            margin-bottom: 15px;
        }

        .progress-label {
            display: flex;
            justify-content: space-between;
            margin-bottom: 8px;
            font-size: 14px;
        }

        .progress-bar {
            height: 8px;
            background: #e9ecef;
            border-radius: 4px;
            overflow: hidden;
        }

        .progress-fill {
            height: 100%;
            background: var(--success);
            border-radius: 4px;
        }

        .task-actions {
            display: flex;
            justify-content: flex-end;
            gap: 10px;
            margin-top: 15px;
        }

        .task-btn {
            padding: 8px 15px;
            border: none;
            border-radius: 6px;
            cursor: pointer;
            font-size: 13px;
            transition: all 0.3s;
            display: flex;
            align-items: center;
        }

        .task-btn .icon {
            margin-right: 5px;
        }

        .task-btn.primary {
            background: var(--primary);
            color: white;
        }

        .task-btn.primary:hover {
            background: var(--secondary);
        }

        .task-btn.default {
            background: #f8f9fa;
            color: var(--dark);
        }

        .task-btn.default:hover {
            background: #e9ecef;
        }

        .empty-state {
            text-align: center;
            padding: 50px 20px;
        }

        .empty-icon {
            font-size: 60px;
            color: #dee2e6;
            margin-bottom: 20px;
        }

        .empty-text {
            font-size: 18px;
            color: var(--gray);
            margin-bottom: 20px;
        }

        /* 响应式设计 */
        @media (max-width: 992px) {
            .sidebar {
                width: 70px;
            }

            .logo-text, .nav-text {
                display: none;
            }

            .logo-icon {
                margin-right: 0;
            }

            .nav-item {
                justify-content: center;
            }

            .nav-icon {
                margin-right: 0;
            }

            .main-content {
                margin-left: 70px;
            }
        }

        @media (max-width: 768px) {
            .task-header {
                flex-direction: column;
                align-items: flex-start;
                gap: 15px;
            }

            .task-tabs {
                width: 100%;
                justify-content: space-between;
            }

            .task-tab {
                flex: 1;
                text-align: center;
                padding: 8px 10px;
            }

            .task-add-btn {
                width: 100%;
                justify-content: center;
            }
        }

        /* 动画效果 */
        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(10px); }
            to { opacity: 1; transform: translateY(0); }
        }

        .fade-in {
            animation: fadeIn 0.5s ease forwards;
        }

        /* 复选框样式 */
        .checkbox {
            width: 18px;
            height: 18px;
            border: 2px solid #ddd;
            border-radius: 4px;
            display: flex;
            align-items: center;
            justify-content: center;
            transition: all 0.3s;
        }

        .checkbox.checked {
            background: var(--success);
            border-color: var(--success);
            color: white;
        }

        /* 侧边栏链接无下划线且不变色 */
        a.nav-item {
            text-decoration: none;
            color: inherit;
        }
        a.nav-item:visited,
        a.nav-item:active,
        a.nav-item:focus {
            color: inherit;
        }
    </style>
</head>
<body>
<div id="app">
    <div class="app-container">
        <!-- 侧边导航 -->
        <div id="sidebar"></div>

        <!-- 主内容区 -->
        <div class="main-content">
            <!-- 顶部导航 -->
            <div class="header">
                <h1 class="page-title">作业</h1>
                <div class="user-info">
                    <div class="avatar">张</div>
                    <div>
                        <div class="user-name">张明</div>
                        <div class="user-role">学生</div>
                    </div>
                </div>
            </div>

            <!-- 任务头部区域 -->
            <div class="task-header">
                <div class="task-tabs">
                    <div
                            class="task-tab"
                            :class="{active: activeTab === 'all'}"
                            @click="activeTab = 'all'">
                        全部任务
                    </div>
                    <div
                            class="task-tab"
                            :class="{active: activeTab === 'pending'}"
                            @click="activeTab = 'pending'">
                        待完成
                    </div>
                    <div
                            class="task-tab"
                            :class="{active: activeTab === 'completed'}"
                            @click="activeTab = 'completed'">
                        已完成
                    </div>
                    <div
                            class="task-tab"
                            :class="{active: activeTab === 'overdue'}"
                            @click="activeTab = 'overdue'">
                        已逾期
                    </div>
                </div>

                <button class="task-add-btn" @click="addTask">
                    <i class="el-icon-plus task-add-icon"></i>
                    添加任务
                </button>
            </div>

            <!-- 任务列表 -->
            <div class="task-list">
                <template v-if="filteredTasks.length > 0">
                    <div
                            class="task-card fade-in"
                            :class="{
                                'urgent': isUrgent(task),
                                'completed': task.completed
                            }"
                            v-for="(task, index) in filteredTasks"
                            :key="index">
                        <div class="task-title">
                            <div
                                    class="checkbox"
                                    :class="{checked: task.completed}"
                                    @click="toggleTaskComplete(task)">
                                <i class="el-icon-check" v-if="task.completed"></i>
                            </div>
                            <span :style="{textDecoration: task.completed ? 'line-through' : 'none'}">
                                    {{ task.title }}
                                </span>
                        </div>

                        <div class="task-course">
                            {{ task.course && task.course.name ? task.course.name : (typeof task.course === 'string' ? task.course : '未知课程') }}
                        </div>

                        <div class="task-meta">
                            <div class="task-meta-item">
                                <i class="el-icon-time task-meta-icon"></i>
                                {{ task.dueDate }}
                            </div>
                            <div class="task-meta-item">
                                <i class="el-icon-s-flag task-meta-icon"></i>
                                {{ task.priority }}
                            </div>
                            <div class="task-meta-item">
                                <i class="el-icon-s-order task-meta-icon"></i>
                                {{ task.type }}
                            </div>
                        </div>

                        <div class="task-description" v-if="task.description">
                            {{ task.description }}
                        </div>

                        <div class="task-progress" v-if="task.progress > 0 && !task.completed">
                            <div class="progress-label">
                                <span>完成进度</span>
                                <span>{{ task.progress }}%</span>
                            </div>
                            <div class="progress-bar">
                                <div class="progress-fill" :style="{width: task.progress + '%'}"></div>
                            </div>
                        </div>

                        <div class="task-actions">
                            <a class="el-button el-button--default" style="margin-right:8px;" 
                               :href="'task-detail.html?assignmentId=' + task.assignmentId">
                               <i class="el-icon-view"></i> 查看详情
                            </a>
                            <button class="task-btn primary" @click="startTask(task)">
                                <i class="el-icon-caret-right icon"></i>
                                {{ task.completed ? '重新开始' : '开始任务' }}
                            </button>
                        </div>
                    </div>
                </template>

                <div class="empty-state" v-else>
                    <i class="el-icon-s-claim empty-icon"></i>
                    <div class="empty-text">没有找到相关任务</div>
                    <el-button type="primary" @click="addTask">添加新任务</el-button>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    new Vue({
        el: '#app',
        data: {
            activeTab: 'all',
            tasks: [] // 改为由API获取
        },
        mounted() {
            fetch('/assignments', {
                headers: {
                    'Authorization': 'Bearer ' + localStorage.getItem('token')
                }
            })
            .then(res => res.json())
            .then(resp => {
                if (resp.success && Array.isArray(resp.data)) {
                    // 为API数据添加样式属性，保持原有的美观效果
                    this.tasks = resp.data.map((task, index) => ({
                        ...task,
                        // 确保优先级字段存在
                        priority: task.priority || this.getTaskPriority(index),
                        // 确保截止日期字段存在
                        dueDate: task.dueDate ? this.formatDate(task.dueDate) : this.getTaskDueDate(index),
                        // 确保进度字段存在
                        progress: task.progress || 0,
                        // 确保完成状态字段存在
                        completed: task.completed || false,
                        // 确保类型字段存在
                        type: task.type || '作业',
                        // 确保课程字段存在（处理课程对象）
                        course: task.course || '通用课程',
                        // 确保描述字段存在
                        description: task.description || '暂无描述',
                        // 计算截止时间戳
                        dueTimestamp: task.dueDate ? new Date(task.dueDate).getTime() : new Date().getTime() + (index + 1) * 24 * 60 * 60 * 1000
                    }));
                } else {
                    console.error('获取任务数据失败:', resp.message);
                    this.tasks = [];
                }
            })
            .catch(error => {
                console.error('请求任务数据出错:', error);
                this.tasks = [];
            });
        },
        computed: {
            filteredTasks() {
                let filtered = this.tasks;

                // 根据活动标签过滤
                switch (this.activeTab) {
                    case 'pending':
                        filtered = filtered.filter(task =>
                            !task.completed && task.dueTimestamp > new Date().getTime()
                        );
                        break;
                    case 'completed':
                        filtered = filtered.filter(task =>
                            task.completed
                        );
                        break;
                    case 'overdue':
                        filtered = filtered.filter(task =>
                            !task.completed && task.dueTimestamp <= new Date().getTime()
                        );
                        break;
                    default:
                        // 'all' 不进行过滤
                        break;
                }

                // 按优先级和截止日期排序
                const priorityOrder = { '紧急': 1, '高': 2, '中': 3, '低': 4 };
                return filtered.sort((a, b) => {
                    // 先按完成状态排序（未完成的在前）
                    if (a.completed !== b.completed) {
                        return a.completed ? 1 : -1;
                    }

                    // 然后按优先级排序
                    if (priorityOrder[a.priority] !== priorityOrder[b.priority]) {
                        return priorityOrder[a.priority] - priorityOrder[b.priority];
                    }

                    // 最后按截止日期排序
                    return a.dueTimestamp - b.dueTimestamp;
                });
            }
        },
        methods: {
            isUrgent(task) {
                if (task.completed) return false;

                const now = new Date().getTime();
                const dueIn24Hours = now + 24 * 60 * 60 * 1000;

                return task.dueTimestamp <= dueIn24Hours && task.dueTimestamp > now;
            },
            toggleTaskComplete(task) {
                task.completed = !task.completed;
                task.progress = task.completed ? 100 : 0;

                this.$message({
                    message: task.completed ? '任务标记为已完成' : '任务标记为未完成',
                    type: 'success'
                });
            },
            viewTask(task) {
                this.$message(`查看任务: ${task.title}`);
                // 实际应用中这里会跳转到任务详情页
            },
            startTask(task) {
                if (task.completed) {
                    task.completed = false;
                    task.progress = 0;
                }
                this.$message(`开始任务: ${task.title}`);
                // 实际应用中这里会跳转到任务执行页面
            },
            addTask() {
                this.$message('添加新任务');
                // 实际应用中这里会打开添加任务对话框
            },
            // 根据索引生成任务优先级
            getTaskPriority(index) {
                const priorities = ['紧急', '高', '中', '低'];
                return priorities[index % priorities.length];
            },
            // 根据索引生成任务截止日期
            getTaskDueDate(index) {
                const now = new Date();
                const days = index + 1;
                const dueDate = new Date(now.getTime() + days * 24 * 60 * 60 * 1000);
                return dueDate.toLocaleDateString('zh-CN');
            },
            // 格式化日期
            formatDate(dateString) {
                if (!dateString) return '';
                try {
                    const date = new Date(dateString);
                    return date.toLocaleDateString('zh-CN') + ' ' + date.toLocaleTimeString('zh-CN', {hour: '2-digit', minute: '2-digit'});
                } catch (e) {
                    return dateString;
                }
            }
        }
    });
</script>
<script>
  fetch('sidebar.html').then(r=>r.text()).then(html=>{document.getElementById('sidebar').innerHTML=html;});
</script>
</body>
</html>